#include "regularshapewidget.h"

RegularShapeWidget::RegularShapeWidget(QWidget *parent) : BHXJGLBaseWidget(parent)
{
    QSurfaceFormat surfaceFormat(this->format());
    surfaceFormat.setSamples(8);
    setFormat(surfaceFormat);

}
RegularShapeWidget::~RegularShapeWidget()
{
    delete  pie;
    delete cone;
    delete sphere;
    delete column;
    delete pipecircle;
}

void RegularShapeWidget::initializeGL()
{
    initializeOpenGLFunctions();

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    shader1.Init(this,":/regvertex.vert",":/regfragment.frag");
    shader.Init(this,":/regvertex1.vert",":/regfragment1.frag",":/reggeomtry1.geom");


    camera.SetPosition(glm::vec3(0.0f, 0.0f, 6.0f));
    camera.PitchLimit();

    projection    = glm::mat4(1.0f);
    float width=static_cast<float>(this->width());
    float height=static_cast<float>(this->height());
    height=height>0?height:1;
    projection = glm::perspective(glm::radians(45.0f), width / height, 0.05f, 100.0f);


    pie=new BHXJGLPie();
    pie->Init(this,32);
    cone=new BHXJGLCone();
    cone->Init(this,32);
    column=new BHXJGLColumn();
    column->Init(this,32);

    sphere=new BHXJGLSphere();
    sphere->Init(this,64,64);

    pipecircle=new BHXJGLPipeCircle();
    pipecircle->Init(this,.3f,16,0.85f,32);

}
void RegularShapeWidget::paintGL()
{
    using namespace glm;

    glEnable(GL_STENCIL_TEST);
    glEnable(GL_DEPTH_TEST);

    glStencilMask(0xFF);//
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
    //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);


    if(_PaintLine)
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    else
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);


    this->shader.use();
    this->shader.setVec3("Color",vec3(0,1,0));

    BHXJGLPaintParam gpp;
    gpp.setProjection(projection);
    gpp.setView(camera.View(true));
    gpp.setPosition(vec3(0,0,-1),true);
    if(this->_PaintNormal)
    {
        this->pie->Paint(gpp,&shader);
    }
    this->shader1.use();
    this->shader1.setVec3("Color",vec3(0,1,0));
    this->pie->Paint(gpp,&shader1);



    gpp.setPosition(vec3(-2,-1,-1));
    gpp.setUp(vec3(0,0,-1));
    gpp.setRight(vec3(1,0,0),true);

    this->shader.use();
    this->shader.setVec3("Color",vec3(1,1,0));
    if(this->_PaintNormal)
    {
        this->cone->Paint(gpp,&shader);
    }
    this->shader1.use();
    this->shader1.setVec3("Color",vec3(1,1,0));
    this->cone->Paint(gpp,&shader1);


    this->shader.use();
    this->shader.setVec3("Color",vec3(0,1,1));
    gpp.setPosition(vec3(2,-1,-1),true);
    if(this->_PaintNormal)
    {
        this->column->Paint(gpp,&shader);
    }
    this->shader1.use();
    this->shader1.setVec3("Color",vec3(0,1,1));
    this->column->Paint(gpp,&shader1);


    this->shader.use();
    this->shader.setVec3("Color",vec3(1,1,1));
    gpp.setUp(vec3(0,1,0));
    gpp.setRight(vec3(1,0,0),true);
    gpp.setPosition(vec3(4,0,-1),true);
    //gpp.setSize(50.f,50.f,50.f,true);
    if(this->_PaintNormal)
    {
        this->sphere->Paint(gpp,&shader);
    }
    this->shader1.use();
    this->shader1.setVec3("Color",vec3(1,1,1));
    this->sphere->Paint(gpp,&shader1);





    this->shader.use();
    this->shader.setVec3("Color",vec3(1,0,1));
    gpp.setPosition(-4,0,-1,true);
    if(this->_PaintNormal)
    {
        this->pipecircle->Paint(gpp,&shader);
    }
    this->shader1.use();
    this->shader1.setVec3("Color",vec3(1,0,1));
    this->pipecircle->Paint(gpp,&shader1);


}

void RegularShapeWidget::resizeGL(int width, int height)
{
    projection    = glm::mat4(1.0f);
    height=height>0?height:1;
    projection = glm::perspective(glm::radians(45.0f), static_cast<float>(width) / static_cast<float>(height), 0.1f, 100.0f);
    // projection = glm::ortho(-(float)width / (float)height, (float)width / (float)height,-1.f, 1.f, 0.1f, 100.0f);
}

